const fs = require("fs");
const path = require("path");
const defaultConfig = require('../config/conf.json')
const { fileExists } = require('../utils/utils')

module.exports = function (inputPath) {
    const userConfigFilePath = path.join(process.env.USERPROFILE, '.aliyunoss.json')
    const ossConfigPath = path.resolve(inputPath)

    if (!fileExists(userConfigFilePath)) {
        fs.writeFileSync(userConfigFilePath, JSON.stringify({
            ...defaultConfig,
            ossConfigPath,
        }, '', '\t'))
    } else {
        const conf = fs.readFileSync(userConfigFilePath, { encoding: 'utf-8' })
        try {
            const confJson = JSON.parse(conf)
            fs.writeFileSync(userConfigFilePath, JSON.stringify({
                ...confJson,
                ossConfigPath,
            }, '', '\t'))
        } catch (error) {
            console.log(error)
        }
    }
}